package com.nci.cloud.unit.management.FilePivotClientByBytes;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.stream.ChunkedFile;

import java.io.*;

public class ClientThread extends Thread {
    // client channel
    private Channel channel;
    private String LR = System.getProperty("line.separator");

    /**
     * the constructor
     * @param channel
     */
    public ClientThread(Channel channel){
        this.channel = channel;
    }// ClientThread

    @Override
    public void run() {
        try {
        BufferedReader in = new BufferedReader(new InputStreamReader(
                System.in));
        while (true) {
                this.sendFileToServer(new File(in.readLine()));
            }// while
        }// try
        catch (IOException e) {
            e.printStackTrace();
        }
    }// run

    public void sendFileToServer(File file ){
        try {
            System.out.println("Channel Active " + channel.id());
            RandomAccessFile localFile = new RandomAccessFile(file, "r");
            System.out.println("file size is "+ localFile.length());
            // zero copy use file region
            DefaultFileRegion fileRegion =  new DefaultFileRegion(localFile.getChannel(), 0, localFile.length());
            ChannelFuture sendFileFuture = channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener(){

                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    channelFuture.channel().close();
                    System.out.println("close the channel");
                }
            });
           /* while(!sendFileFuture.isSuccess()){
            //    System.out.println(sendFileFuture.isSuccess());
            }*/

            //sSystem.out.println(sendFileFuture.isSuccess());

            //channelHandlerContext.write(cf);
     /*   ChannelFuture sendFileFuture;
        sendFileFuture = */

        /*sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
                if (total < 0) { // total unknown
                    System.out.println(future.channel() + " Transfer progress: " + progress);
                } else {
                    System.out.println(future.channel() + " Transfer progress: " + progress + " / " + total);
                }
            }
            @Override
            public void operationComplete(ChannelProgressiveFuture future) {
                System.out.println(future.channel() + " Transfer complete.");
                channel.close();
            }
        });*/

            System.out.println("send the file");
            // don't close the channel the file won't be received completely.

        }
        catch (Exception e){
            e.printStackTrace();
        }// catch
    }// sendFileToServer


}
